﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
using System.Data;
using KPIS.Utils;
using KPIS.DBM;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.MODEL;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.APP.Classes;


namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PR_SalaryCommandDetail : System.Web.UI.UserControl
    {
        private object _dataItem = null;
        Command_BLL cmdBLL = new Command_BLL();
        private SsMng ssMng = new SsMng();
        Main main = new Main();

         public void IncludeJava()
         {
             HtmlGenericControl script = new HtmlGenericControl();
            script.TagName = "script";
            script.Attributes.Add("type", "text/javascript");
            script.Attributes.Add("src", "../../Scripts/jsfunction.js");
            Page.Header.Controls.Add(script);
         }
        //ReadOnly property.
        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }
        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }
        //Initializing the new variable updating ViewState data.
        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);
            IncludeJava();
            txtOldSalary.Attributes.Add("onkeypress", "return isNumberKeyDot(event)");
            txtNewSalary.Attributes.Add("onkeypress", "return isNumberKeyDot(event)");
            if (!IsUserControlPostBack)
            {
                this.ViewState.Add("IsUserControlPostBack", true);
            }

            if (DataItem != null)
            {
                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    btnFindEmp.Visible = false;
                    lblID.Text = DataBinder.Eval(DataItem, "cmd_dtls_seq").ToString();

                    string name = DataBinder.Eval(DataItem, "first_name").ToString();
                    name += "  " + DataBinder.Eval(DataItem, "last_name").ToString();

                    txtEmployee.Text = name; //first_name,last_name
                    lblPosRef.Text = DataBinder.Eval(DataItem, "new_pos_ref").ToString();
                    lblPosName.Text = DataBinder.Eval(DataItem, "new_pos_name").ToString();
                    lblClassName.Text = DataBinder.Eval(DataItem, "class_name").ToString();
                    lblJobSiteName.Text = DataBinder.Eval(DataItem, "new_job_site_name").ToString();
                    txtOldSalary.Text = DataBinder.Eval(DataItem, "old_salary").ToString();
                    txtNewSalary.Text = DataBinder.Eval(DataItem, "new_salary").ToString();
                
                    string mydate = DataBinder.Eval(DataItem, "start_date").ToString();  // --------  4/15/2009 12:00:00 AM
                    if (mydate != "")
                    {
                        string[] str = mydate.Split('/');
                        str[2] = (int.Parse(str[2].Substring(0, 4)) + 543).ToString();
                        mydate = str[1] + "/" + str[0] + "/" + str[2];
                        txtDate.SetValue(mydate);
                    }
                    this.btnAddnew.Visible = false;
                    this.btnUpdate.Visible = true;
                }
                else
                {
                    this.btnUpdate.Visible = false;
                    this.btnAddnew.Visible = true;
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
 
        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            try
            {
                string mydate = txtDate.GetStringValue();  //18/03/2553
                mydate = Utils.Date.ToEngFormatDate(mydate);
                string id = Session["id"].ToString();
                string[] str = id.Split('|');


                bool chkDup;
                chkDup = cmdBLL.ChkDuplicate(int.Parse(lblID_emp.Text), str[0]);

                if (chkDup != true)
                    cmdBLL.InsertSalaryCommandDetail(int.Parse(str[0]), int.Parse(lblID_emp.Text), DateTime.Parse(mydate), int.Parse(lblID_pos.Text), lblPosRef.Text, lblPosName.Text, int.Parse(lblID_class.Text), int.Parse(lblID_jobSite.Text), lblJobSiteName.Text, Double.Parse(txtNewSalary.Text), Double.Parse(txtOldSalary.Text), ssMng.EmpSeq);
                else
                    main.MessageAlert(this.Page, "Duplicate!!");
            }
            catch(Exception ex)
            {
                
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                string mydate = txtDate.GetStringValue();  //18/03/2553
                mydate = Utils.Date.ToEngFormatDate(mydate);

                cmdBLL.UpdateSalaryCommandDetail(int.Parse(lblID.Text), DateTime.Parse(mydate), Double.Parse(txtNewSalary.Text), Double.Parse(txtOldSalary.Text), ssMng.EmpSeq);
            }
            catch (Exception ex)
            {

            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        private IEmployeeBLL empBLL = new EmployeeBLL();
        protected void btnFindEmp_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                string st = Hide.Value;
              
                if (st == "")
                    return;

                EmployeeInfo infoEmp;
                IList<EmployeeInfo> infoList = empBLL.ListEmployees(st);
                infoEmp = infoList[0];

                lblID_emp.Text = infoEmp.Sequence.ToString();

                lblID_pos.Text = infoEmp.Position.Sequence.HasValue == true ? infoEmp.Position.Sequence.ToString() : "";

                lblID_class.Text = infoEmp.Class.Sequence != null ? infoEmp.Class.Sequence.ToString() : "";

                lblID_jobSite.Text = infoEmp.WorkingJobSite.Sequence.HasValue == true ? infoEmp.WorkingJobSite.Sequence.ToString() : "";

                txtEmployee.Text = infoEmp.Person.FullName;
                lblPosRef.Text = infoEmp.Position.RefNo;
                lblPosName.Text = infoEmp.Position.Name;
                lblClassName.Text = infoEmp.Position.ClassSpecification.Name;
                lblJobSiteName.Text = infoEmp.WorkingJobSite.Name;
                txtOldSalary.Text = infoEmp.Salary.ToString();

            }
            catch
            { 
            
            }
        }

        protected void btnFindEmp_Load(object sender, EventArgs e)
        {
	        string id = Session["id"].ToString();
            string[] str;
            str = id.Split('|');


            btnFindEmp.Attributes.Add("OnClick", "LinkMDdetail(" + Hide.ClientID + "," + "'Forms/DialogEmp.aspx?ETID=" + str[1] + "'" + ",'../frmIframe.html',700,500)");
        }
    }
}